#include "../../common.h"
#include <stdio.h>
#include <string.h>

void test_sm3_hash() {
	    printf("=== Testing SM3 Hash ===\n");
	        
	        const char *password = "mySecurePassword123";
		    char salt[SM3_DIGEST_LENGTH];
		        unsigned char hash1[SM3_DIGEST_LENGTH], hash2[SM3_DIGEST_LENGTH];
			    
			    generate_salt(salt, sizeof(salt));
			        compute_sm3_hash(password, salt, hash1);
				    compute_sm3_hash(password, salt, hash2);
				        
				        if(memcmp(hash1, hash2, SM3_DIGEST_LENGTH) == 0) {
						        printf("SM3 Hash Test PASSED\n");
							    } else {
								            printf("SM3 Hash Test FAILED\n");
									        }
					    
					    char diff_salt[SM3_DIGEST_LENGTH];
					        generate_salt(diff_salt, sizeof(diff_salt));
						    compute_sm3_hash(password, diff_salt, hash2);
						        
						        if(memcmp(hash1, hash2, SM3_DIGEST_LENGTH) != 0) {
								        printf("SM3 Salt Difference Test PASSED\n");
									    } else {
										            printf("SM3 Salt Difference Test FAILED\n");
											        }
}

void test_sm4_encryption() {
	    printf("\n=== Testing SM4 Encryption ===\n");
	        
	        unsigned char key[16] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
			                            0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10};
		    const char *plaintext = "This is a secret message!";
		        size_t len = strlen(plaintext);
			    
			    unsigned char ciphertext[256];
			        unsigned char decrypted[256];
				    
				    sm4_encrypt(key, (unsigned char *)plaintext, ciphertext, len);
				        sm4_decrypt(key, ciphertext, decrypted, len);
					    
					    if(memcmp(plaintext, decrypted, len) == 0) {
						            printf("SM4 Encryption/Decryption Test PASSED\n");
							        } else {
									        printf("SM4 Encryption/Decryption Test FAILED\n");
										        printf("Original: %s\n", plaintext);
											        printf("Decrypted: %s\n", decrypted);
												    }
					        
					        const char *short_text = "Hi";
						    sm4_encrypt(key, (unsigned char *)short_text, ciphertext, 2);
						        sm4_decrypt(key, ciphertext, decrypted, 2);
							    
							    if(memcmp(short_text, decrypted, 2) == 0) {
								            printf("SM4 Short Message Test PASSED\n");
									        } else {
											        printf("SM4 Short Message Test FAILED\n");
												    }
}

void test_message_structs() {
	    printf("\n=== Testing Message Structs ===\n");
	        
	        if(sizeof(MessageHeader) == 8) {
			        printf("MessageHeader size test PASSED\n");
				    } else {
					            printf("MessageHeader size test FAILED (expected 8, got %zu)\n", sizeof(MessageHeader));
						        }
		    
		    LoginRequest login_req;
		        strncpy(login_req.username, "testuser", MAX_USERNAME_LEN);
			    memset(login_req.password_hash, 0xAA, SM3_DIGEST_LENGTH);
			        
			        printf("LoginRequest sample:\n");
				    printf("Username: %s\n", login_req.username);
				        printf("Password hash: %02x...%02x\n", 
							           login_req.password_hash[0], 
								              login_req.password_hash[SM3_DIGEST_LENGTH-1]);
}

int main() {
	    test_sm3_hash();
	        test_sm4_encryption();
		    test_message_structs();
		        return 0;
}
